package bb.lanxing.util;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;

import com.umeng.socialize.net.dplus.CommonNetImpl;

public class StartInBackgroundPermissionUtil {
    public static boolean canBackgroundStart(Context context) {
        try {
            if (Build.VERSION.SDK_INT >= 19) {
                return (Integer) AppOpsManager.class.getDeclaredMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class).invoke((AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE), 10021, Binder.getCallingUid(), context.getPackageName()) == 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void requestBackgroundStartPermission(Context context) {
        Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR_PRIVATE");
        intent.putExtra("extra_pkgname", context.getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}